package com.aim.controller.system;

import com.aim.common.annotion.Permission;
import com.aim.common.base.AjaxResult;
import com.aim.common.util.PageUtil;
import com.aim.common.util.WebUtil;
import com.aim.entity.system.SysLoginLog;
import com.aim.service.system.SysLoginLogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @AUTO 登陆日志控制器
 * @Author AIM
 * @DATE 2019/4/26
 */
@Controller
@RequestMapping("/loginLog")
public class SysLogController {

	private static String PREFIX = "system/log/";

	@Autowired
	private SysLoginLogService loginLogService;

	/**
	 * 列表页面
	 */
	@RequestMapping("")
	public String index() {
		return PREFIX + "loginList.html";
	}

	/**
	 * 查询列表
	 */
	@PostMapping("/list")
	@Permission
	@ResponseBody
	public Object list(HttpServletRequest request) {
		Map<String, Object> params = WebUtil.getRequestMap(request);
		PageHelper.startPage(PageUtil.getPageNum(params), PageUtil.getPageSize(params));
		List<SysLoginLog> list = loginLogService.findList(params);
		PageInfo<SysLoginLog> pageInfo = new PageInfo<SysLoginLog>(list);
		return AjaxResult.success((int) pageInfo.getTotal(), list);
	}

	@PostMapping("/clear")
	@Permission
	@ResponseBody
	public Object clearLog() {
		loginLogService.deleteTable();
		return AjaxResult.success();
	}

}
